//
//  NewFeatureViewController.swift
//  新浪微博
//
//  Created by mac on 15/11/29.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
let maximagecount: Int = 4

class NewFeatureViewController: UICollectionViewController {
    
    init(){
        let layout = UICollectionViewFlowLayout()
        //设置cell大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //调用super
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
    
   
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return maximagecount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
        cell.index = indexPath.item
        // Configure the cell
    
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //indexpath 是上一个显示的cell对应的indexpath
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        let indexItem = index?.item ?? 0
        if indexItem == maximagecount - 1 {
        cell.startAnimation()
        }
        
        
    }
    
    //MARK 自定义cell
    class NewFeatureCell: UICollectionViewCell{
        //定义属性index
        var index: Int = 0 {
            didSet{
            iconView.image = UIImage(named: "new_feature_\(index+1)")
                startBtn.hidden = true
            }
        }
        
        //重写初始化方法
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        //设置布局
        private func setupUI(){
        //添加子视图
            contentView.addSubview(iconView)
             contentView.addSubview(startBtn)
            //设置布局
            iconView.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(contentView.snp_edges)
            }
            startBtn.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(contentView.snp_centerX)
                make.bottom.equalTo(contentView.snp_bottom).offset(-180)
            }
            //添加点击事件
            startBtn.addTarget(self, action: "startBtnClick", forControlEvents: .TouchUpInside)
            
        }
     @objc  private func startBtnClick(){
        //发出切换页面消息
            NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
         
        }
        func startAnimation(){
            //动画开始之前隐藏按钮
           startBtn.hidden = true
        //设置初始形变
            startBtn.transform = CGAffineTransformMakeScale(0, 0)
            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
              self.startBtn.hidden = false
                self.startBtn.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    print("ok")
            }
        }
    //懒加载子视图
        private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
        private lazy var startBtn: UIButton = {
            let btn = UIButton()
            btn.setTitle("开始体验", forState: .Normal)
            btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
            btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        return btn
        }()
    
    }
   
}
